var req = require("./action.js"),utils = require("./util.js")
var app = getApp()
function getPhoneNumber(e,callback){
  console.log(e);
  if(e.detail.errMsg == 'getPhoneNumber:ok'){
    let r = {encryptedData:e.detail.encryptedData,iv:e.detail.iv,sessionKey:wx.getStorageSync('sessionKey'),customerId:wx.getStorageSync('customerId')}
    let _url = req.actions.ym+"customer/getPhoneNumber";
    let that = this;
    utils.myResquest(r,_url,function(res){
      console.log(res.data)
      if(res.data.code == '200'){
        let phoneNum = res.data.content
        wx.setStorageSync('phone', phoneNum)
        callback(phoneNum);
      }else if(res.data.code == '406'){
        //登录过期
        app.login(function(res){
          if(res == 'complete'){
            console.log(res)
            utils.myResquest(r,_url,function(res){
              console.log(res.data)
              if(res.data.code == '200'){
                let phoneNum = res.data.content
                wx.setStorageSync('phone', phoneNum)
                callback(phoneNum);
              }
            });
          }
        })
      }else{
        wx.showModal({
          title: '登陆异常'
        })
        callback('fail');
      }
    });
  }else{
    wx.showModal({
      title: '授权失败'
    })
    callback('fail');
  }
}

module.exports = {
  getPhoneNumber: getPhoneNumber
}